package com.scuec.trainingmanagementsys.mapper;

import com.scuec.trainingmanagementsys.entity.Administrator;
import com.scuec.trainingmanagementsys.entity.Teacher;
import org.apache.ibatis.annotations.Delete; // 添加 Delete 注解的导入语句
import org.apache.ibatis.annotations.Insert; // 同样，若使用了 Insert 注解，也需要导入
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface AdministratorMapper {

    @Select("SELECT * FROM administrator " +
            "WHERE AdministratorName = #{username} " +
            "AND AdministratorPassword = #{password} " +
            "AND AdministratorType = #{type}")  // 新增类型条件
    Administrator selectByUsernamePasswordAndType(
            @Param("username") String username,
            @Param("password") String password,
            @Param("type") String type
    );

    // 1. 整表查询
    @Select("SELECT * FROM administrator")
    List<Administrator> selectAll();

    // 2. 姓名模糊查询（使用CONCAT实现百分号拼接）
    @Select("SELECT * FROM administrator " +
            "WHERE AdministratorName LIKE CONCAT('%', #{name}, '%')")
    List<Administrator> selectByName(@Param("name") String name);

    // 3. 删除单条记录（根据ID）
    @Delete("DELETE FROM administrator WHERE AdministratorID = #{id}")
    int deleteById(@Param("id") Integer id);

    // 4. 批量删除（使用FOREACH标签处理IN条件）
    @Delete({
            "<script>",
            "DELETE FROM administrator",
            "WHERE AdministratorID IN",
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    int deleteBatch(@Param("ids") List<Integer> ids);

    // 5. 插入记录（字段与实体属性一一对应）
    @Insert("INSERT INTO administrator (" +
            "AdministratorName, AdministratorPassword, Email, Phone, AdministratorType" +
            ") VALUES (" +
            "#{admin.administratorName}, #{admin.administratorPassword}, #{admin.email}, #{admin.phone}, #{admin.administratorType}" +
            ")")
    int insert(@Param("admin") Administrator admin);

    // 6. 修改记录（根据ID更新非空字段，不更新AdministratorType）
    @Update({
            "<script>",
            "UPDATE administrator ",
            "SET ",
            "<if test='admin.administratorName != null'>AdministratorName = #{admin.administratorName},</if>",
            "<if test='admin.administratorPassword != null'>AdministratorPassword = #{admin.administratorPassword},</if>",
            "<if test='admin.email != null'>Email = #{admin.email},</if>",
            "<if test='admin.phone != null'>Phone = #{admin.phone}</if>",
            "WHERE AdministratorName = #{admin.administratorName}",
            "</script>"
    })
    int update(@Param("admin") Administrator admin);

    // 根据工号和密码查询教师
    @Select("SELECT * FROM teacher WHERE TeacherNumber = #{number} AND TeacherPassword = #{password}")
    Teacher getByNumberAndPassword(@Param("number") String number, @Param("password") String password);

    @Select("SELECT COUNT(*) FROM administrator")
    int getAdministratorCount();

    // 根据管理员账号查询管理员信息
    @Select("SELECT * FROM administrator WHERE AdministratorName = #{administratorName}")
    Administrator selectByAdministratorName(@Param("administratorName") String administratorName);

    // 根据管理员账号更信息
    @Update({
            "<script>",
            "UPDATE administrator ",
            "SET ",
            "<if test='admin.administratorPassword != null'>AdministratorPassword = #{admin.administratorPassword},</if>",
            "<if test='admin.email != null'>Email = #{admin.email},</if>",
            "<if test='admin.phone != null'>Phone = #{admin.phone}</if>",
            "WHERE AdministratorName = #{admin.administratorName}",
            "</script>"
    })
    int updateByAdministratorName(@Param("admin") Administrator admin);
}